import time

import requests


class PatchDistribution:

    def test_patch_distribution(self, vmids, serviceGroupId, serviceGroupVersion):
        # 50个一批
        grouped_vmids = [vmids[i:i + 20] for i in range(0, len(vmids), 20)]
        for group in grouped_vmids:
            # 去后台管理系统查询接口  实例是否可用 得到可用的实例列表
            effective_vmids = PatchDistribution.query_effective_vmids(",".join(list(map(lambda x: str(x), group))))
            # 调服务组分发接口 得到可分发的实例列表
            instances = PatchDistribution.quert_instance_list(effective_vmids)
            # 调用服务组分发接口
            PatchDistribution.create_release_task(serviceGroupId, serviceGroupVersion, instances)
            if len(group) == 20:
                print(f"分发20个完成")
                time.sleep(180)
            else:
                print(f"分发{len(group)}个完成")
                return

    @staticmethod
    def query_effective_vmids(vmids):
        url = f"http://cgmgr.vrviu.com:19997/api/vmInfo/getCESVMInfoList?page=1&pageSize=500&vmid__in={vmids}&zoneID=4103&online_state__in=4&game_info_version__in=&is_local_feild=0"
        headers = {
            "x-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVVUlEIjoiNjFiZmY3NzUtMmQwZi00NDNhLTk0ODYtZTFlY2Y4MzdiNWNmIiwiSUQiOjQxMSwiVXNlcm5hbWUiOiJmcmFuay55YW4iLCJOaWNrTmFtZSI6ImZyYW5rLnlhbiIsIkF1dGhvcml0eUlkIjo4ODgsIkJ1ZmZlclRpbWUiOjg2NDAwLCJleHAiOjE3NDEyMzUxOTEsImlzcyI6InFtUGx1cyIsIm5iZiI6MTc0MDUzNTA5M30.C2b6QUBXqcLqxHlKabnNKEi6ZPlGBpIpkSRKlwzEtfs",
            "x-user-id": "411"
        }
        resp = requests.get(url=url, headers=headers)
        if resp.status_code != 200:
            raise Exception("状态码失败")
        effective_vmids = list(map(lambda x: x.get('vmid'), resp.json().get('data').get('list')))
        return effective_vmids

    @staticmethod
    def quert_instance_list(vmids):
        url = "https://cgboss.yuntiancloud.com/api/cginstance/v1/instance/list"
        headers = {
            "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEzMTk3LCJpc3MiOiJjZ2Jvc3MiLCJleHAiOjE3NDI2MDk4MTksIm5iZiI6MTc0MDAxNzgxOSwiaWF0IjoxNzQwMDE3ODE5fQ.tQYa9MoMW39iSclrugtA2LaRSmMt0H3ZG4NOpdienhg"
        }
        data = {
            "vmids": vmids,
            "projectIds": [],
            "resourceType": 1,
            "arches": ["X86"],
            "page": {
                "size": 100,
                "no": 1
            },
            "orders": [{
                "field": "updatedAt",
                "type": "DESC"
            }]
        }
        resp = requests.post(url=url, headers=headers, json=data)
        if resp.status_code != 200:
            raise Exception("状态码失败")
        instances = list(map(lambda x: x.get('gInstanceId'), resp.json().get("body").get('instances')))
        return instances

    @staticmethod
    def create_release_task(serviceGroupId=None, serviceGroupVersion=None, instances=None):
        url = "https://cgboss.yuntiancloud.com//api/cgbundle/v1/service_groups/create_release_task"
        headers = {
            "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEzMTk3LCJpc3MiOiJjZ2Jvc3MiLCJleHAiOjE3NDI2MDk4MTksIm5iZiI6MTc0MDAxNzgxOSwiaWF0IjoxNzQwMDE3ODE5fQ.tQYa9MoMW39iSclrugtA2LaRSmMt0H3ZG4NOpdienhg"
        }
        data = {"serviceGroupId": serviceGroupId, "serviceGroupVersion": serviceGroupVersion,
                "instanceIds": instances,
                "releaseNote": "逆水寒手游pcf分发"}
        resp = requests.post(url=url, headers=headers, json=data)
        if resp.status_code != 200:
            raise Exception("状态码失败")


if __name__ == '__main__':
    vmids = [48210, 48211, 48213, 48214, 48215, 48216, 48218, 48220, 48224, 48226, 48228, 48229,
             48230, 48231, 48232, 48237, 48239, 48240, 48241, 48244, 48245, 48247, 48248, 48251, 48252, 48253, 48256,
             48258, 48259, 48262, 48265, 48267, 48270, 48271, 48274, 48276, 48278, 48281, 48283, 48286, 48287, 48288,
             48289, 48291, 48295, 48296, 48299, 48303, 48305, 48306, 48309, 48310, 48311, 48314, 48316, 48317, 48319,
             48320, 48322, 48323, 48325, 48327, 48328, 48331, 48333, 48335, 48336, 48337, 48341, 48342, 48350, 48351,
             48355, 48358, 48360, 48363, 48364, 48365, 48367, 48368, 48370, 48375, 48378, 48380, 48381, 48382, 48383,
             48384, 48387, 48388, 48389, 48393, 48395, 48396, 48401, 48402, 48403, 48404, 48405, 48406, 48414, 48415,
             48416, 48417, 48418, 48419, 48421, 48423, 48424, 48425, 48429, 48432, 48433, 48434, 48435, 48436, 48437,
             48438, 48439, 48442, 48446, 48447, 48450, 48452, 48453, 48454, 48455, 48457, 48459, 48460, 48461, 48462,
             48463, 48465, 48466, 48468, 48469, 48470, 48479, 48481, 48482, 48483, 48484, 48485, 48486, 48487, 48488,
             48490, 48492, 48493, 48494, 48495, 48496, 48499, 48501, 48504, 48508, 48510, 48511, 48513, 48514, 48515,
             48517, 48518, 48519, 48520, 48521, 48522, 48525, 48526, 48531, 48533, 48534, 48535, 48538, 48539, 48540,
             48541, 48545, 48547, 48548, 48549, 48551, 48552, 48553, 48555, 48556, 48561, 48562, 48563, 48565, 48566,
             48569, 48571, 48573, 48575, 48579, 48582, 48587, 48589, 48590, 48591, 48593, 48594, 48596, 48597, 48598,
             48599, 48604, 48606, 48608, 48609, 48610, 48611, 48615, 48617, 48618, 48619, 48621, 48622, 48623, 48624,
             48625, 48626, 48627, 48629, 48632, 48633, 48635, 48636, 48640, 48641, 48642, 48643, 48645, 48646, 48647,
             48651, 48652, 48653, 48654, 48660, 48661, 48662]
    PatchDistribution().test_patch_distribution(vmids, 34, "1.0.2")
